/*
 * Gui.java
 *
 * Created on July 15, 2006, 1:06 PM
 */

package net.anzix.jimposition;

import java.awt.CardLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.DefaultListModel;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import net.anzix.jimposition.transformations.BookletFilter;
import net.anzix.jimposition.transformations.BorderFilter;
import net.anzix.jimposition.transformations.CutStackFilter;
import net.anzix.jimposition.transformations.ImpositionFilter;
import net.anzix.jimposition.transformations.PageFilter;
import net.anzix.jimposition.transformations.ParityFilter;
import net.anzix.jimposition.transformations.ResizeFilter;
import net.anzix.jimposition.transformations.RotateFilter;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;


public class Gui extends javax.swing.JFrame {
    private Logger logger = Logger.getLogger(Gui.class);
    /*private File inputFile = null;
    private File outputFile = null;*/
    
    private File inputFile;
    private File outputFile;
    
    private List<Transformation> availableProcessors = new Vector<Transformation>();
    private DefaultListModel activeProcessors = new DefaultListModel();
    /** Creates new form Gui */
    public Gui() {
        availableProcessors.add(new PageFilter());
        availableProcessors.add(new ImpositionFilter());
        availableProcessors.add(new ResizeFilter());
        availableProcessors.add(new BookletFilter());
        availableProcessors.add(new BorderFilter());
        availableProcessors.add(new RotateFilter());
        availableProcessors.add(new ParityFilter());
        availableProcessors.add(new CutStackFilter());
        initComponents();
        
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        inputFileName = new javax.swing.JTextField();
        BrowseButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        settingsPanel = new javax.swing.JPanel();
        jButton4 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        activeFilterList = new javax.swing.JList();
        filterComboBox = new javax.swing.JComboBox();
        addButton = new javax.swing.JButton();
        removeButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("jImposition");
        setResizable(false);
        inputFileName.setEditable(false);
        inputFileName.setText("choose a file");

        BrowseButton.setText("Browse");
        BrowseButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BrowseButtonActionPerformed(evt);
            }
        });

        jLabel1.setText("Input file:");

        settingsPanel.setLayout(new java.awt.CardLayout());

        settingsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Transformation Settings"));

        jButton4.setText("Save the new pdf");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Transformations"));
        activeFilterList.setModel(activeProcessors);
        activeFilterList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                activeFilterListValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(activeFilterList);

        filterComboBox.setModel(new DefaultComboBoxModel((Vector)availableProcessors));

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        removeButton.setText("Remove");
        removeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(addButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 119, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(removeButton, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 233, Short.MAX_VALUE)
            .add(filterComboBox, 0, 233, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(filterComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(addButton)
                    .add(removeButton)))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(17, 17, 17)
                        .add(inputFileName, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 362, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(BrowseButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 152, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(settingsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 348, Short.MAX_VALUE))
                    .add(jButton4, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 597, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(inputFileName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 19, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(BrowseButton))
                .add(6, 6, 6)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(settingsPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton4)
                .addContainerGap(14, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void activeFilterListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_activeFilterListValueChanged
        if (activeFilterList.getSelectedValue()==null) return;
        CardLayout cl = (CardLayout)(settingsPanel.getLayout());
        cl.show(settingsPanel, ((Transformation)activeFilterList.getSelectedValue()).getId());
        settingsPanel.revalidate();
        settingsPanel.repaint();
    }//GEN-LAST:event_activeFilterListValueChanged
    
    private void BrowseButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BrowseButtonActionPerformed
        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(Gui.this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            inputFile = fc.getSelectedFile();
            inputFileName.setText(inputFile.getAbsolutePath());
        }
    }//GEN-LAST:event_BrowseButtonActionPerformed
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        
        
        if (inputFile==null || !inputFile.exists()){
            JOptionPane.showMessageDialog(this,"Input file not found","ERROR",JOptionPane.ERROR_MESSAGE);
        } else {
            
            JFileChooser fc;
            if (outputFile!=null && outputFile.exists()){
                fc = new JFileChooser(outputFile);
                fc.setName(outputFile.getName());
            } else {
                fc = new JFileChooser();
            }
            fc.setApproveButtonText("Save");
            
            int returnVal = fc.showOpenDialog(Gui.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                outputFile = fc.getSelectedFile();
                Collection<Transformation> transformations = new ArrayList();
                
                for (int i=0;i<activeProcessors.getSize();i++){
                    Transformation transform = (Transformation)activeProcessors.get(i);
                    transform.getPanelSettings();
                    transformations.add(transform);
                }
                logger.debug("begin process");
                try {
                    Processor.processor(transformations,inputFile,outputFile);
                } catch (FileNotFoundException ex) {
                    JOptionPane.showMessageDialog(this,"Input file not found","ERROR",JOptionPane.ERROR_MESSAGE);
                    ex.printStackTrace();
                }
            }
            
        }
        
    }//GEN-LAST:event_jButton4ActionPerformed
    
    private void removeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeButtonActionPerformed
        //settingsPanel.remo
                /*settingsPanel.get.add(proc.getPanel(),""+activeProcessors.size());
            settingsPanel.revalidate();
            settingsPanel.repaint();*/
        /*        Processor proc = (Processor)activeFilterList.getSelectedValue();*/
        CardLayout cl = (CardLayout)(settingsPanel.getLayout());
        System.out.println(activeFilterList.getSelectedIndex());
        //activeProcessors.getElementAt(activeFilterList.getSelectedIndex());
        activeProcessors.removeElement(activeFilterList.getSelectedValue());
        //activeProcessors.remove(activeFilterList.getSelectedIndex());
        //cl.removeLayoutComponent(((Processor)activeProcessors.remove(activeFilterList.getSelectedIndex())).getPanel());
        activeFilterList.revalidate();
        activeFilterList.repaint();
        activeFilterList.setSelectedIndex(1);
        
    }//GEN-LAST:event_removeButtonActionPerformed
    
    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        try{
            Transformation proc = (Transformation)filterComboBox.getSelectedItem().getClass().newInstance();
            settingsPanel.add(proc.getPanel(),proc.getId());
            activeProcessors.addElement(proc);
            settingsPanel.revalidate();
            settingsPanel.repaint();
            repaint();
        } catch(InstantiationException ex){
            ex.printStackTrace();
            
        } catch(IllegalAccessException ex){
            ex.printStackTrace();
        }
        
    }//GEN-LAST:event_addButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        BasicConfigurator.configure();
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Gui().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BrowseButton;
    private javax.swing.JList activeFilterList;
    private javax.swing.JButton addButton;
    private javax.swing.JComboBox filterComboBox;
    private javax.swing.JTextField inputFileName;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton removeButton;
    private javax.swing.JPanel settingsPanel;
    // End of variables declaration//GEN-END:variables
    
   
    
   
}
